<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2015/8/28 0028
  Time: 上午 9:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML>

<html>
<head>
    <title>资源列表</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%--<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">--%>
    <link href="/static/bui/css/bs3/dpl.css" rel="stylesheet" type="text/css"/>
    <link href="/static/bui/css/bs3/bui.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="doc-content">
    <div class="panel">
        <div class="panel-header">
            <h3>资源信息</h3>
        </div>
        <div class="panel-body">
            <form id="resourceForm" class="form-horizontal well" action="/resource/find.do" method="post">
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label">资源名称：</label>

                        <div class="controls">
                            <input type="text" class="control-text" name="name">
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label">web请求：</label>

                        <div class="controls">
                            <input type="text" class="control-text" name="url">
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label">资源描述：</label>

                        <div class="controls">
                            <input type="text" class="control-text" name="type">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label">资源分组：</label>

                        <div class="controls">
                            <input name="group" type="text" data-rules="{required:true}"
                                   class="input-normal control-text">
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label">资源状态：</label>

                        <div class="controls">
                            <select name="available">
                                <option value="">请选择</option>
                                <option value="1">正常</option>
                                <option value="0">禁止</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="span3 offset2">
                    <input value="搜索" type="submit" id="btnSearch" class="button button-primary"/>
                </div>
        </form>

        <div class="row">
            <div class="span21 offset3 control-row-auto">
                <div id="grid"></div>
                <input type="hidden" name="eduation">
            </div>
        </div>

        <div id="resource_content" class="hide">
            <form id="resource_Form" class="form-horizontal bui-form-horizontal bui-form bui-form-field-container">
                <div class="row" style="display: none">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>_id：</label>

                        <div class="controls">
                            <input name="_id" type="text" class="input-normal control-text bui-form-field"
                                   aria-disabled="false" aria-pressed="false">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>资源名称：</label>

                        <div class="controls">
                            <input name="name" type="text" data-rules="{required:true}"
                                   class="input-normal control-text bui-form-field" aria-disabled="false"
                                   aria-pressed="false">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>web请求：</label>

                        <div class="controls">
                            <input name="url" type="text" data-rules="{required:true}"
                                   class="input-normal control-text">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>资源描述：</label>

                        <div class="controls">
                            <input name="type" type="text" data-rules="{required:true}"
                                   class="input-normal control-text">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label">资源状态：</label>

                        <div class="controls">
                            <select name="available" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <option value="1">正常</option>
                                <option value="0">禁止</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>资源分组：</label>

                        <div class="controls">
                            <select name="group" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <option value="0">新建组</option>
                                <c:forEach items="${group}" var="p">
                                    <option value="${p}">${p}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <script type="text/javascript" src="/static/bui/js/jquery-1.8.1.min.js"></script>
        <script type="text/javascript" src="/static/bui/js/bui.js"></script>
        <script type="text/javascript" src="/static/bui/js/config.js"></script>
        <%--<script type="text/javascript">--%>
        <%--BUI.use('common/page');--%>
        <%--</script>--%>
        <script type="text/javascript">
            BUI.use(['bui/grid', 'bui/data', 'bui/form'], function (Grid, Data, Form) {
                var columns = [
                            {title: '资源名称', dataIndex: 'name', width: '25%'},
                            {title: 'web请求', dataIndex: 'url', width: '35%'},
                            {title: '资源描述', dataIndex: 'type', width: '15%'},
                            {title: '状态', dataIndex: 'available', width: '10%'},
                            {title: '分组', dataIndex: 'group', width: '10%'},
                            {
                                title: '操作', width: '25%', renderer: function () {
                                var editStr = '<span class="grid-command btn-edit"><i class="icon-edit"></i>编辑</span>';
                                return editStr
                            }
                            }
                        ],
                //默认的数据
                        data = [
                            <c:forEach items="${resources}" var="p">
                            {
                                _id: '${p._id}',
                                name: '${p.name}',
                                url: '${p.url}',
                                type: '${p.type}',
                                available: '<c:if test="${p.available==1}">正常</c:if><c:if test="${p.available==0}">禁止</c:if>',
                                group: '${p.group}'
                            },
                            </c:forEach>
                        ],
                        store = new Data.Store({
                            data: data,
                            pageSize: 20
                        }),
                        editing = new Grid.Plugins.DialogEditing({
                            contentId: 'resource_content',
                            triggerCls: 'btn-edit',
                            editor: {
                                focusable: false,
                                success: function () {
                                    var editType = editing.get('editType'),
                                            edtor = this,
                                            form = edtor.get("form");
                                    form.valid();
                                    if (form.isValid()) {
                                        this.close();
                                        if (editType == 'add') {
                                            $.ajax({
                                                type: 'post',
                                                url: '/resource/addResource',
                                                data: $("#resource_Form").serialize(),
                                                success: function (msg) {
                                                    if (msg == 'success') {
                                                        msg = "添加成功！";
                                                        BUI.Message.Alert(msg, function () {
                                                            window.location.href = '/resource/list.do';
                                                        }, 'success')
                                                    } else {
                                                        msg = "添加失败！";
                                                        BUI.Message.Alert(msg, function () {
                                                            window.location.href = '/jsp/error/noAuthority.jsp';
                                                        }, 'error')
                                                    }

                                                }
                                            })
                                        } else {
                                            $.ajax({
                                                type: 'post',
                                                url: '/resource/updateResource',
                                                data: $("#resource_Form").serialize(),
                                                success: function (msg) {
                                                    if (msg == 'success') {
                                                        msg = '修改成功！';
                                                        BUI.Message.Alert(msg, function () {
                                                            window.location.href = '/resource/list.do';
                                                        }, 'success')
                                                    } else {
                                                        msg = '修改失败！';
                                                        BUI.Message.Alert(msg, function () {
                                                            window.location.href = '/jsp/error/noAuthority.jsp';
                                                        }, 'error')
                                                    }
                                                }
                                            })
                                        }
                                    }
                                }
                            }
                        }),
                        grid = new Grid.Grid({
                            render: '#grid',
                            columns: columns,
                            width: '100%',
//                    forceFit: true,
                            plugins: [Grid.Plugins.AutoFit],
                            store: store,
                            bbar: {
                                pagingBar: true,
                                elCls: 'image-pbar'
                            },
                            plugins: [Grid.Plugins.CheckSelection, editing],
                            tbar: {
                                items: [{
                                    btnCls: 'button button-small',
                                    text: '<i class="icon-plus"></i>添加',
                                    listeners: {
                                        'click': addFunction
                                    }
                                },
                                    {
                                        btnCls: 'button button-small',
                                        text: '<i class="icon-remove"></i>删除',
                                        listeners: {
                                            'click': delFunction
                                        }
                                    },
                                    {
                                        btnCls: 'button button-small',
                                        text: '<i class="icon-plus"></i>导出表格',
                                        listeners: {
                                            'click': exportFunction
                                        }
                                    }]
                            }
                        });
                grid.render();

                grid.on('cellclick', function (ev) {
                    var record = ev.record,
                            target = $(ev.domTarget);
                    if (target.hasClass('btn-info')) {
                        window.location.href = '/resource/findResource?_id=' + record._id;
                    }
                })

                function exportFunction() {
                    window.location.href = "/resource/export.do";
                }

                function addFunction() {
                    var newData = {};
                    editing.add(newData); //添加记录后，直接编辑
                }

                function delFunction() {
                    var selections = grid.getSelection();
                    delItems(selections);
                }

                function delItems(items) {
                    var _ids = [];
                    BUI.each(items, function (item) {
                        _ids.push(item._id)
                    });
                    if (_ids.length) {
                        BUI.Message.Confirm('确认要删除吗？', function () {
                            $.ajax({
                                type: 'POST',
                                url: '/resource/delResource?_ids=' + _ids,
                                success: function (msg) {
                                    if (msg == 'success') {
                                        msg = "删除成功！"
                                    }
                                    BUI.Message.Alert(msg, function () {
                                        window.location.href = '/resource/list.do';
                                    }, 'success')
                                }
                            })
                        })
                    }
                };
                var form = new Form.HForm({
                    srcNode: '#resource_Form'
                });
            });
        </script>
    </div>
</div>
</div>
</body>
</html>
